package com.example.demo.controller;

import com.example.demo.Constants;
import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import com.example.demo.model.Result;
import com.example.demo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2025-05-10
 * Time: 21:51
 */
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/addBook")
    public String AddBook(BookInfo bookInfo){
        log.info("添加图书：{}",bookInfo);
        if (bookInfo == null
                || !StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null
                || bookInfo.getPrice()==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus()==null) {
            return "参数错误";
        }
        try {
            Integer result =bookService.addBook(bookInfo);
            if (result==1){
                return "";
            }
        }catch (Exception e){
            log.error("添加图书失败 e {}",e);
            return "内部错误";
        }
        return "内部错误";
    }
    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> PageResult(PageRequest pageRequest, HttpSession session){
        log.info("请求图书列表 PageRequest：{}",pageRequest);
        if (!StringUtils.hasLength((String)session.getAttribute(Constants.SESSION_LOGIN_USER_NAME))){
            return Result.unlogin();
        }
        return Result.success(bookService.getListByPage(pageRequest));
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryById(Integer bookId){
        log.info("查找的id是{}",bookId);
        return bookService.queryBookById(bookId);
    }
    @RequestMapping("updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("修改的数据是{}",bookInfo);
        Integer result= bookService.updateBook(bookInfo);
        try {
            if (result<1){
                return "图书更新失败";
            }else {
                return "";
            }
        }catch (Exception e){
            log.error("图书更新失败",e);
            return "图书更新失败";
        }
    }

    @RequestMapping("deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除的数据是{}",bookId);
        Integer result= bookService.deleteBook(bookId);
        try {
            if (result<1){
                return "图书删除失败";
            }else {
                return "";
            }
        }catch (Exception e){
            log.error("图书删除失败",e);
            return "图书删除失败";
        }
    }
    @RequestMapping("/batchDeleteBook")
    public Integer batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书 ids{}",ids);
        return bookService.batchDeleteBook(ids);
    }
}
